2. Prepare input data of Yoshii River¶
Written by Men Vuthy, 2022
Import modules
[1]:
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import rasterio
import geopandas as gpd
Label
[2]:
yoshii_class_img = rasterio.open('data/yoshii_river/out_img/class/yoshii_class.tiff')
[3]:
yoshii_class = yoshii_class_img.read(1).reshape(-1)
[4]:
yoshii_label = pd.DataFrame({'label':yoshii_class})
Dataframe of Season 1
[5]:
season1_rgbn = rasterio.open('data/yoshii_river/out_img/rgbn/yoshii_20190409_rgbn.tiff')
season1_ndvi = rasterio.open('data/yoshii_river/out_img/ndvi/yoshii_20190409_ndvi.tiff')
season1_ndwi = rasterio.open('data/yoshii_river/out_img/ndwi/yoshii_20190409_ndwi.tiff')
season1_bsi = rasterio.open('data/yoshii_river/out_img/bsi/yoshii_20190409_bsi.tiff')
[6]:
blue_1 = season1_rgbn.read(1).reshape(-1)
green_1 = season1_rgbn.read(2).reshape(-1)
red_1 = season1_rgbn.read(3).reshape(-1)
nir_1 = season1_rgbn.read(4).reshape(-1)
ndvi_1 = season1_ndvi.read(1).reshape(-1)
ndwi_1 = season1_ndwi.read(1).reshape(-1)
bsi_1 = season1_bsi.read(1).reshape(-1)
[7]:
Season_1 = pd.DataFrame({'B1':blue_1, 'G1':green_1, 'R1':red_1, 'NIR1':nir_1, 'NDVI1':ndvi_1, 'NDWI1':ndwi_1, 'BSI1':bsi_1})
[8]:
Season_1
[8]:
| B1 | G1 | R1 | NIR1 | NDVI1 | NDWI1 | BSI1 | |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 1 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 2 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 3 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 4 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 38283940 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283941 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283942 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283943 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283944 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
38283945 rows × 7 columns
Dataframe of Season 2
[9]:
season2_rgbn = rasterio.open('data/yoshii_river/out_img/rgbn/yoshii_20190805_rgbn.tiff')
season2_ndvi = rasterio.open('data/yoshii_river/out_img/ndvi/yoshii_20190805_ndvi.tiff')
season2_ndwi = rasterio.open('data/yoshii_river/out_img/ndwi/yoshii_20190805_ndwi.tiff')
season2_bsi = rasterio.open('data/yoshii_river/out_img/bsi/yoshii_20190805_bsi.tiff')
[10]:
blue_2 = season2_rgbn.read(1).reshape(-1)
green_2 = season2_rgbn.read(2).reshape(-1)
red_2 = season2_rgbn.read(3).reshape(-1)
nir_2 = season2_rgbn.read(4).reshape(-1)
ndvi_2 = season2_ndvi.read(1).reshape(-1)
ndwi_2 = season2_ndwi.read(1).reshape(-1)
bsi_2 = season2_bsi.read(1).reshape(-1)
[11]:
Season_2 = pd.DataFrame({'B2':blue_2, 'G2':green_2, 'R2':red_2, 'NIR2':nir_2, 'NDVI2':ndvi_2, 'NDWI2':ndwi_2, 'BSI2':bsi_2})
[12]:
Season_2
[12]:
| B2 | G2 | R2 | NIR2 | NDVI2 | NDWI2 | BSI2 | |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 1 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 2 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 3 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 4 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 38283940 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283941 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283942 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283943 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283944 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
38283945 rows × 7 columns
Dataframe of Season 3
[13]:
season3_rgbn = rasterio.open('data/yoshii_river/out_img/rgbn/yoshii_20191007_rgbn.tiff')
season3_ndvi = rasterio.open('data/yoshii_river/out_img/ndvi/yoshii_20191007_ndvi.tiff')
season3_ndwi = rasterio.open('data/yoshii_river/out_img/ndwi/yoshii_20191007_ndwi.tiff')
season3_bsi = rasterio.open('data/yoshii_river/out_img/bsi/yoshii_20191007_bsi.tiff')
[14]:
blue_3 = season3_rgbn.read(1).reshape(-1)
green_3 = season3_rgbn.read(2).reshape(-1)
red_3 = season3_rgbn.read(3).reshape(-1)
nir_3 = season3_rgbn.read(4).reshape(-1)
ndvi_3 = season3_ndvi.read(1).reshape(-1)
ndwi_3 = season3_ndwi.read(1).reshape(-1)
bsi_3 = season3_bsi.read(1).reshape(-1)
[15]:
Season_3 = pd.DataFrame({'B3':blue_3, 'G3':green_3, 'R3':red_3, 'NIR3':nir_3, 'NDVI3':ndvi_3, 'NDWI3':ndwi_3, 'BSI3':bsi_3})
[16]:
Season_3
[16]:
| B3 | G3 | R3 | NIR3 | NDVI3 | NDWI3 | BSI3 | |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 1 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 2 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 3 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 4 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 38283940 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283941 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283942 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283943 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283944 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
38283945 rows × 7 columns
Dataframe of Season 4
[17]:
season4_rgbn = rasterio.open('data/yoshii_river/out_img/rgbn/yoshii_20200104_rgbn.tiff')
season4_ndvi = rasterio.open('data/yoshii_river/out_img/ndvi/yoshii_20200104_ndvi.tiff')
season4_ndwi = rasterio.open('data/yoshii_river/out_img/ndwi/yoshii_20200104_ndwi.tiff')
season4_bsi = rasterio.open('data/yoshii_river/out_img/bsi/yoshii_20200104_bsi.tiff')
[18]:
blue_4 = season4_rgbn.read(1).reshape(-1)
green_4 = season4_rgbn.read(2).reshape(-1)
red_4 = season4_rgbn.read(3).reshape(-1)
nir_4 = season4_rgbn.read(4).reshape(-1)
ndvi_4 = season4_ndvi.read(1).reshape(-1)
ndwi_4 = season4_ndwi.read(1).reshape(-1)
bsi_4 = season4_bsi.read(1).reshape(-1)
[19]:
Season_4 = pd.DataFrame({'B4':blue_4, 'G4':green_4, 'R4':red_4, 'NIR4':nir_4, 'NDVI4':ndvi_4, 'NDWI4':ndwi_4, 'BSI4':bsi_4})
[20]:
Season_4
[20]:
| B4 | G4 | R4 | NIR4 | NDVI4 | NDWI4 | BSI4 | |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 1 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 2 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 3 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 4 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 38283940 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283941 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283942 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283943 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
| 38283944 | 0 | 0 | 0 | 0 | NaN | NaN | NaN |
38283945 rows × 7 columns
Features and Label
[21]:
# concat dataframe of all seasons and label into one dataframe
Yoshii_dataframe = pd.concat([Season_1, Season_2, Season_3, Season_4, yoshii_label], axis=1)
[22]:
# Remove all rows where NDVI1 is NaN
Yoshii_classified = Yoshii_dataframe.dropna(subset=['NDVI1'])
[23]:
Yoshii_classified
[23]:
| B1 | G1 | R1 | NIR1 | NDVI1 | NDWI1 | BSI1 | B2 | G2 | R2 | ... | NDWI3 | BSI3 | B4 | G4 | R4 | NIR4 | NDVI4 | NDWI4 | BSI4 | label | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 4371353 | 558 | 601 | 513 | 889 | -0.023003 | 0.081056 | 0.279785 | 324 | 421 | 385 | ... | -0.054566 | 0.235740 | 349 | 489 | 457 | 686 | 0.135323 | -0.030485 | 0.222090 | 7 |
| 4371354 | 558 | 601 | 513 | 889 | -0.023003 | 0.081056 | 0.279785 | 324 | 421 | 385 | ... | -0.054566 | 0.235740 | 349 | 489 | 457 | 686 | 0.135323 | -0.030485 | 0.222090 | 7 |
| 4371355 | 614 | 639 | 533 | 1012 | -0.006031 | 0.047061 | 0.282591 | 334 | 440 | 420 | ... | -0.118004 | 0.259052 | 387 | 540 | 571 | 843 | 0.127158 | -0.083739 | 0.253888 | 7 |
| 4371356 | 705 | 715 | 606 | 1203 | 0.011312 | 0.016838 | 0.292253 | 391 | 507 | 509 | ... | -0.222598 | 0.262350 | 458 | 627 | 705 | 1027 | 0.120572 | -0.107544 | 0.273603 | 1 |
| 4371357 | 816 | 794 | 681 | 1472 | 0.039086 | -0.031651 | 0.304678 | 501 | 614 | 648 | ... | -0.257952 | 0.264777 | 557 | 733 | 838 | 1254 | 0.133792 | -0.128988 | 0.285978 | 1 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 34076326 | 896 | 883 | 753 | 2833 | 0.309235 | -0.296701 | 0.300410 | 473 | 619 | 575 | ... | -0.482353 | 0.253913 | 492 | 664 | 795 | 1464 | 0.233941 | -0.251074 | 0.292699 | 1 |
| 34076327 | 919 | 904 | 775 | 2700 | 0.275665 | -0.263721 | 0.301996 | 484 | 628 | 582 | ... | -0.464620 | 0.253741 | 489 | 667 | 818 | 1428 | 0.208541 | -0.237249 | 0.296889 | 1 |
| 34076328 | 949 | 929 | 803 | 2563 | 0.236333 | -0.226434 | 0.304925 | 523 | 659 | 616 | ... | -0.424649 | 0.263849 | 519 | 704 | 875 | 1397 | 0.165456 | -0.201109 | 0.301471 | 1 |
| 34076329 | 964 | 951 | 827 | 2445 | 0.206467 | -0.192717 | 0.304421 | 559 | 695 | 657 | ... | -0.351897 | 0.268711 | 507 | 736 | 859 | 1422 | 0.183002 | -0.188259 | 0.271648 | 1 |
| 34076330 | 1010 | 989 | 869 | 2362 | 0.167315 | -0.156990 | 0.308427 | 615 | 750 | 733 | ... | -0.318919 | 0.269634 | 573 | 791 | 897 | 1501 | 0.188206 | -0.179563 | 0.274091 | 1 |
1152991 rows × 29 columns
[24]:
# Data dir
data_dir = "data/yoshii_river/out_img/classified"
# Output dataframe
out_df = os.path.join(data_dir, 'yoshii_classified.csv')
# export dataframe to csv
Yoshii_classified.to_csv(out_df, index=False)
[25]:
# Data dir
data_dir = "data/yoshii_river/out_img/classified"
# Output dataframe
out_df = os.path.join(data_dir, 'yoshii_classified_index.csv')
# export dataframe to csv
Yoshii_classified.to_csv(out_df)